package m.store.action;

import m.base.action.BaseAction;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.attr.TagColor;
import m.html.element.other.Param;
import m.html.form.*;
import m.html.list.*;
import m.store.model.GuestBackAmount;
import m.store.service.GuestBackAmountService;
import m.store.service.IncomeAmountService;
import m.store.util.ColorUtil;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.util.DictionaryUtil;

import java.util.Date;

@ActionMeta(name = "store/guestBackAmount")
public class GuestBackAmountAction extends BaseAction {
    private GuestBackAmount model;
    /**
     * 保存
     */
    @RequestMeta(transaction = true,logTitle = "货款往来",logDescription = "保存货款往来")
    public JSONMessage doSave() throws Exception {
        model.setStore(PowerUtil.getSessionStore(true));
        String msg=getService(GuestBackAmountService.class).save(model);
        return new JSONMessage(0,msg)
                .push("model", model);
    }
    @RequestMeta(transaction = true,logTitle = "货款往来",logDescription = "删除货款往来")
    public JSONMessage doDelete() throws Exception {
        getService(GuestBackAmountService.class).delete(model.getOid());
        return new JSONMessage(0,"删除成功");
    }
    @RequestMeta(transaction = true,logTitle = "货款往来",logDescription = "拆分货款往来")
    public JSONMessage doSplit() throws Exception {
        getService(GuestBackAmountService.class).split(model.getOid());
        return new JSONMessage(0,"拆分成功");
    }
    /*
     * 编辑到款登记
     */
    public VueResult toEdit() throws Exception {
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","guest.name");
        }else if(null!=model&&!StringUtil.isSpace(model.getType())){
            model.setBackDate(new Date());
            model.setLinkStatus(GuestBackAmount.LinkStatus_No);
            model.setBusiness(GuestBackAmount.Business_Yes);
        }else{
            throw new MException(this.getClass(),"参数错误");
        }
        ModelForm form = new ModelForm(DictionaryUtil.getName("m.store.model.GuestBackAmount!type",model.getType())+"信息");
        form.addItem(
            FormInput.selectOneModel("客户", "model","guest","'action/store/guestInfo/selectOneGuest'",1300,"<a @click=\"viewGuest\">{{model.guest.name}}</a>").setSpan(2),
            FormInput.radio("主营业务","model.business","m.store.model.GuestBackAmount!business"),
            FormInput.select("支付方式", "model.mode", "m.store.model.GuestBackAmount!mode").setPlaceholder("请选择支付方式"),
            FormInput.number("支付金额", "model.backAmount").setPlaceholder("请输入支付金额"),
            FormInput.date("支付时间", "model.backDate").setPlaceholder("请输入支付时间"),
            FormInput.textarea("备注", "model.remark").setSpan(3).setPlaceholder("请输入备注")
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .success("backFlag"),
            FormEvent.toModal("viewGuest",actionUrl(GuestInfoAction.class,"toView"),"{model:{oid:this.model.guest.oid}}",1300)
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    /*
     * 到款登记列表
     */
    public VueResult inputList() throws Exception {
        String type=getParamsValue("type");
        if(StringUtil.isSpace(type)) throw new MException(this.getClass(),"参数错误");
        VueListResult result=getVueListResult(DictionaryUtil.getName("m.store.model.GuestBackAmount!type",type) +"登记",GuestBackAmount.class,actionUrl("inputList"));
        result.setFunction(true,false);
        result.setHeight(-1);
        result.setFocusRefresh(true);
        result.addCol(toIndexColumn());
        result.addCol(toLinkColumn("客户名称", "guest.name", 150, true,
            new ListEvent(actionUrl(GuestInfoAction.class,"toView"), EventType.MODAL,1100)
                .addParam("model.oid","guest.oid")
        ));
        result.addCol(toElementColumn("支付方式", "mode", 100,
            ListElement.tag("m.store.model.GuestBackAmount!mode", ColorUtil.getAmountMode(),false)
        ).setDictType("m.store.model.GuestBackAmount!mode").setFilter(true));
        result.addCol(toNumberColumn("支付金额", "backAmount", 100, true));
        result.addCol(toDateColumn("支付时间", "backDate", 100, true));
        result.addCol(toTextColumn("备注", "remark", 150, true));
        result.addCol(toTextColumn("主营业务","business",100,true).setDictOption(GuestBackAmount.class,"business"));
        result.addCol(toElementColumn("关联状态", "linkStatus", 100,
            ListElement.dot("m.store.model.GuestBackAmount!linkStatus",ColorUtil.getAmountLinkStatus())
        ).setDictType("m.store.model.GuestBackAmount!linkStatus").setFilter(true));
        result.addCol(toNumberColumn("关联金额", "linkAmount", 100, true));
        result.addCol(toSpaceColumn("操作", 100,
            ListElement.test("record.linkStatus=='"+GuestBackAmount.LinkStatus_No+"'",
                ListButton.modal("修改", ButtonType.PRIMARY,actionUrl("toEdit"),1000, SuccessMethod.REFRESH)
                    .addParam("model.oid","oid"),
                ListButton.ajax("删除", ButtonType.ERROR,actionUrl("doDelete"), SuccessMethod.REFRESH)
                    .addParam("model.oid","oid").setConfirm("确定要删除该记录吗？")
            ),
            ListElement.test("record.linkStatus=='"+GuestBackAmount.LinkStatus_linking+"'",
                ListButton.ajax("拆分{{formatNumber(record.backAmount-record.linkAmount,'#,##0.00')}}元", ButtonType.WARN,actionUrl("doSplit"), SuccessMethod.REFRESH)
                    .addParam("model.oid","oid").setConfirm("确定要把剩余金额拆分成新记录吗？")
            )
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,actionUrl("toEdit"),1000, SuccessMethod.REFRESH)
                .addQueryParam(Param.value("model.type",type))
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("type",type));
        result.addCondition(QueryCondition.eq("store.oid", PowerUtil.getSessionStoreOid()));
        return result;
    }
    public GuestBackAmount getModel() {
        return model;
    }

    public void setModel(GuestBackAmount model) {
        this.model = model;
    }
}
